package handler

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"sale_system/order_srv/global"
	"sale_system/order_srv/model"
	"sale_system/order_srv/proto"
)

func (s *OrderServer) CartItemList(ctx context.Context, request *proto.UserInfo) (*proto.CartItemListResponse, error) {

	var shopCartsGoods []model.ShoppingCart // 用户购物车的内的所有商品

	result := global.DB.Where(&model.ShoppingCart{User: request.Id}).Find(&shopCartsGoods)
	if result.Error != nil {
		return nil, result.Error
	}

	var rsp proto.CartItemListResponse
	rsp.Total = int32(result.RowsAffected)

	for _, shopCartsGood := range shopCartsGoods {

		rsp.Data = append(rsp.Data, &proto.ShopCartInfoResponse{
			Id:      shopCartsGood.ID,
			UserId:  shopCartsGood.User,
			GoodsId: shopCartsGood.Goods,
			Nums:    int32(shopCartsGood.Nums),
			Checked: shopCartsGood.Checked,
		})
	}
	return &rsp, nil
}
func (s *OrderServer) CreateCartItem(ctx context.Context, request *proto.CartItemRequest) (*proto.ShopCartInfoResponse, error) {
	// 将商品添加到购物车 1.原本没有这件商品  2.购物车已经有了，再加入就是增加对应的数量
	var shopCartGood model.ShoppingCart

	// 注意查询条件为，谁的购物车的什么商品
	result := global.DB.Where(&model.ShoppingCart{Goods: request.GoodsId, User: request.UserId}).Find(&shopCartGood)
	if result.Error != nil {
		return nil, result.Error
	}
	// 表示当前用户购物车内并没有这件商品，进行新增
	if result.RowsAffected == 0 {

		shopCartGood.Goods = request.GoodsId
		shopCartGood.User = request.UserId
		shopCartGood.Nums = int32(request.Nums)
		shopCartGood.Checked = false // 加入默认是false

		result = global.DB.Save(&shopCartGood)
		if result.RowsAffected == 0 {
			return nil, status.Error(codes.Internal, "新建失败")
		}
	} else { // 当前用户购物车内有这个数据，那么创建就相当于合并购物车数据，然后保存

		shopCartGood.Nums += request.Nums
		result = global.DB.Save(&shopCartGood)
		if result.RowsAffected == 0 {
			return nil, status.Error(codes.Internal, "合并失败")
		}
	}

	return &proto.ShopCartInfoResponse{Id: shopCartGood.ID}, nil

}
func (s *OrderServer) UpdateCartItem(ctx context.Context, request *proto.CartItemRequest) (*emptypb.Empty, error) {

	// 购物车已经有了，再加入就是增加对应的数量
	var shopCartGood model.ShoppingCart

	// 注意查询条件为，谁的购物车的什么商品
	result := global.DB.Where("goods=? and user=?", request.GoodsId, request.UserId).First(&shopCartGood)
	if result.Error != nil {
		return nil, result.Error
	}

	// 注意数目一定要大于0，其实我们前端可以控制 > 0，保证传入后端一定大于0
	if request.Nums > 0 {
		shopCartGood.Nums = int32(request.Nums)
	}
	shopCartGood.Checked = request.Checked // 加入默认是false

	result = global.DB.Save(&shopCartGood)
	if result.RowsAffected == 0 {
		return nil, status.Error(codes.Internal, "更新失败")
	}

	return &emptypb.Empty{}, nil
}
func (s *OrderServer) DeleteCartItem(ctx context.Context, request *proto.CartItemRequest) (*emptypb.Empty, error) {

	result := global.DB.Where("goods=? and user=?", request.GoodsId, request.UserId).Delete(&model.ShoppingCart{})
	if result.RowsAffected == 0 {
		return nil, status.Error(codes.Internal, "购物车记录不存在")
	}
	return &emptypb.Empty{}, nil

}
